import { getToken, setToken, removeToken } from '@/utils/auth'
import { loginAPI, getUserInofAPI, personalnformationAPI } from '@/api/user'
// import resetRouter from '@/router/index'
const state = {
  token: getToken(),
  userinof: {}
}

const mutations = {
  // user login
  setToken(store, token) {
    store.token = token
    setToken(token)
  },
  // user inof
  setUserinof(store, inof) {
    store.userinof = inof
  },
  // 用户基本信息
  setEmployeeBasicInformation(store, userinof) {
    // 合并用户信息
    store.state.userinof = { ...store.state.userinof, ...userinof }
  },

  // remove token
  removeToken(store) {
    store.token = null
    removeToken()
    store.userinof = {}
  }
  // remove userinof

}
// 登录请求
const actions = {
  async login(store, data) {
    const res = await loginAPI(data)
    store.commit('setToken', res.data)
  },
  // 获取用户信息
  async  userinof(store) {
    const res = await getUserInofAPI()

    // 获取用户基本信息
    const resp = await personalnformationAPI(res.data.userId)

    store.commit('setUserinof', { ...res.data, ...resp.data })
    console.log(resp)
    return res.data.roles
  }

}

// get user info

// user logout

export default {
  namespaced: true,
  state,
  mutations,
  actions

}

